package com.haochang.chunkvod.view.vod.dialog;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * @author Yangjie
 *         className ChooseActivityDialog
 *         created at  2016/8/27  17:37
 *         单选dialog，左边边显示 已选
 */

public class ChooseActivityDialog extends Dialog
{
	public ChooseActivityDialog(Context context)
	{
		super(context);
	}

	public ChooseActivityDialog(Context context, int theme)
	{
		super(context, theme);
	}

	protected ChooseActivityDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
	{
		super(context, cancelable, cancelListener);
	}

	public static class Builder
	{
		private DialogEnum dialogEnum = DialogEnum.DEFAULT;
		private Context context;
		private CharSequence title; //对话框标题
		private List<ActivityDialogItemInfo> dataList;
		private List<ActivityDialogItemInfo> dataListTemp = new ArrayList<ActivityDialogItemInfo>();
		private OnSingleChoosedListner mOnSelectListener; //对话框点击事件
		private QuickAdapter quickAdapter = null;

		public Builder(Context context)
		{
			this.context = context;
		}

		public Builder setTitle(int title)
		{
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setTitle(CharSequence title)
		{
			this.title = title;
			return this;
		}

		public Builder setDataList(List<ActivityDialogItemInfo> dataList)
		{
			this.dataList = dataList;
			return this;
		}

		public Builder dialogEnum(DialogEnum dialogEnum)
		{
			this.dialogEnum = dialogEnum;
			return this;
		}

		public List<ActivityDialogItemInfo> getDataList()
		{
			return dataList;
		}

		//设置单选List选项及事件，这些属性在之后的createDialog中用到，这里使用Android系统创建dialog的风格
		public Builder setSingleChoiceItems(List<ActivityDialogItemInfo> dataList, final OnSingleChoosedListner listener)
		{
			this.dataList = dataList;
			for (ActivityDialogItemInfo info : dataList)
			{
				ActivityDialogItemInfo info1 = new ActivityDialogItemInfo();
				List<BaseDialogItemInfo> list = new ArrayList<BaseDialogItemInfo>();
				for (BaseDialogItemInfo childInfo : info.getList())
				{
					BaseDialogItemInfo infoChild = new BaseDialogItemInfo();
					infoChild.setCode(childInfo.getCode());
					infoChild.setContent(childInfo.getContent());
					infoChild.setName(childInfo.getName());
					infoChild.setSelected(childInfo.isSelected());
					list.add(infoChild);
				}
				info1.setList(list);
				info1.setSelected(info.isSelected());
				info1.setName(info.getName());
				info1.setContent(info.getContent());
				info1.setCode(info.getCode());
				dataListTemp.add(info1);
			}
			this.mOnSelectListener = listener;
			return this;
		}

		private boolean clickEnsure = false;
		private LinearLayout layout;//inflate的view

		/**
		 * 根据规定的最大条数显示列表
		 *
		 * @param rows 最大显示条数
		 * @return ChooseActivityDialog
		 */
		public ChooseActivityDialog createDialog(int rows)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final ChooseActivityDialog dialog = new ChooseActivityDialog(context, R.style.Alert_Dialog);
			final ViewGroup viewGroup = null;
			layout = (LinearLayout) inflater.inflate(R.layout.inflate_choose_activity_dialog, viewGroup, false);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			BaseListView listView = (BaseListView) layout.findViewById(R.id.listView);
			if (rows == 0)
			{
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 40 * dataList.size())));
			} else
			{
				//当数据条数小于设置最大显示条数时，高度设置为实际高度，当大于设置条数时，显示设置条数的高度
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 41 * rows)));
			}
			quickAdapter = new QuickAdapter<ActivityDialogItemInfo>(context, R.layout.item_activity, dataList)
			{
				@Override
				protected void convert(final BaseAdapterHelper helper, final ActivityDialogItemInfo item)
				{
					ImageView iv_checked = helper.getView(R.id.iv_checked);
					LinearLayout linear_add = helper.getView(R.id.linear_add);
					List<BaseDialogItemInfo> list = item.getList();
					helper.setText(R.id.tv_name, item.getName());
					helper.setText(R.id.tv_content, item.getContent());
					linear_add.removeAllViews();
					if (linear_add.getChildCount() <= 0 && list != null && list.size() > 0)
					{
						int len = list.size();
						for (int i = 0; i < len; i++)
						{
							View view = LayoutInflater.from(context).inflate(R.layout.item_activity_child, null, false);
							View spaceLine = view.findViewById(R.id.spaceLine);
							BaseTextView tv_name = (BaseTextView) view.findViewById(R.id.tv_name);
							BaseTextView tv_content = (BaseTextView) view.findViewById(R.id.tv_content);
							tv_name.setText(list.get(i).getName());
							tv_content.setText(list.get(i).getContent());
							LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
							if (i == len - 1)
							{
								spaceLine.setBackgroundColor(context.getResources().getColor(R.color.color_bbbbbb));
								params.setMargins(DipPxConversion.dipToPx(context, 14), 0, DipPxConversion.dipToPx(context, 14), 0);
							} else
							{
								spaceLine.setBackgroundColor(context.getResources().getColor(R.color.color_dddddd));
								params.setMargins(DipPxConversion.dipToPx(context, 26), 0, DipPxConversion.dipToPx(context, 26), 0);
							}
							spaceLine.setLayoutParams(params);
							linear_add.addView(view);
						}
					}
					if (item.isSelected())
					{
						iv_checked.setImageResource(R.drawable.rb_checked);
						itemTemp = item;
					} else
					{
						iv_checked.setImageResource(R.drawable.rb_unchecked);
					}
					helper.getView().setOnClickListener(new OnBaseClickListener()
					{
						@Override
						public void onBaseClick(View v)
						{
							itemTemp = item;
							onRefresh(item);
							quickAdapter.notifyDataSetChanged();
						}
					});
				}
			};
			listView.setAdapter(quickAdapter);
			BaseTextView tv_title = (BaseTextView) layout.findViewById(R.id.tv_title);
			tv_title.setText(title);
			layout.findViewById(R.id.tv_ensure).setOnClickListener(new OnBaseClickListener()
			{
				@Override
				public void onBaseClick(View v)
				{
					clickEnsure = true;
					if (mOnSelectListener != null)
					{
						if (itemTemp != null)
						{
							mOnSelectListener.onSingleChoosed(itemTemp);
						}
						dialog.dismiss();
					}
				}
			});
			layout.findViewById(R.id.tv_cancel).setOnClickListener(new OnBaseClickListener()
			{
				@Override
				public void onBaseClick(View v)
				{
					clickEnsure = false;
					dialog.dismiss();
				}
			});
			dialog.setOnDismissListener(new OnDismissListener()
			{
				@Override
				public void onDismiss(DialogInterface dialogInterface)
				{
					if (!clickEnsure)
					{
						dataList.clear();
						dataList.addAll(dataListTemp);
					}
				}
			});
			return dialog;
		}

		public void showDialog(int rows)
		{
			ChooseActivityDialog dialog = createDialog(rows);
			dialog.show();
			Window window = dialog.getWindow();
			if (window != null)
			{
				WindowManager.LayoutParams layoutParams = window.getAttributes();
				layoutParams.width = ScreenUtils.getScreenWidth(context) - 2 * DipPxConversion.dip2px(context, 20);
				window.setAttributes(layoutParams);
				window.setGravity(Gravity.CENTER);
				window.setBackgroundDrawable(new ColorDrawable(0));
				window.setContentView(layout);
				dialog.setCancelable(true);
				dialog.setCanceledOnTouchOutside(false);
			}
		}

		ActivityDialogItemInfo itemTemp = null;

		/**
		 * 刷新数据
		 *
		 */
		private void onRefresh(ActivityDialogItemInfo info)
		{
			for (ActivityDialogItemInfo itemInfo : dataList)
			{
				if (itemInfo.getName().equals(info.getName()))
				{
					itemInfo.setSelected(true);
				} else
				{
					itemInfo.setSelected(false);
				}
			}

		}

	}

	public interface OnSingleChoosedListner
	{
		void onSingleChoosed(ActivityDialogItemInfo info);
	}

	public enum DialogEnum
	{
		/**
		 * 默认类型，无图标
		 */
		DEFAULT,
		/**
		 * 支付类型，带图标
		 */
		PAY,
		/**
		 * 特殊收款
		 */
		SPECIAL_PAY
	}
}
